package com.drinks.yiniang.adapter.user.address;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.entity.user.address.DeliverAddressJson;
import com.hansen.library.Constants;
import com.hansen.library.utils.StringUtils;

import java.util.List;

/**
 * 收货地址adapter
 * @author han
 * @date 2019/8/30
 */
public class DeliverAddressAdapter extends BaseQuickRCVAdapter<DeliverAddressJson.DeliverAddressData, BaseViewHolder> {

    private Drawable mNormalDrawable;
    private Drawable mCheckDrawable;
    private StringBuilder mBuilder;

    public DeliverAddressAdapter(Context mContext, @Nullable List<DeliverAddressJson.DeliverAddressData> data) {
        super(R.layout.item_deliver_address, data);

        mCheckDrawable = ContextCompat.getDrawable(mContext, R.mipmap.picker_orignal_checked);
        mNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.picker_orignal_normal);
    }

    @Override
    protected void convert(BaseViewHolder helper, DeliverAddressJson.DeliverAddressData item) {
        //收货人
        mBuilder = new StringBuilder();
        mBuilder.append(mContext.getString(R.string.text_receiver_colon));
        mBuilder.append(StringUtils.getNullEmptyConvert__(item.getReal_name()));
        helper.setText(R.id.tv_deliver_address_receiver, mBuilder);

        //电话
        helper.setText(R.id.tv_deliver_address_phone, StringUtils.getNullEmptyConvert__(item.getMobile()));

        //地址
        mBuilder = new StringBuilder();
        mBuilder.append(mContext.getString(R.string.text_deliver_address_colon));
        mBuilder.append(item.getRegion_province() != null ? StringUtils.getNullEmptyConvert__(item.getRegion_province().getRegion_name()) : "");
        mBuilder.append(" ");
        mBuilder.append(item.getRegion_city() != null ? StringUtils.getNullEmptyConvert__(item.getRegion_city().getRegion_name()) : "");
        mBuilder.append(" ");
        mBuilder.append(item.getRegion_district() != null ? StringUtils.getNullEmptyConvert__(item.getRegion_district().getRegion_name()) : "");
        mBuilder.append(" ");
        mBuilder.append(StringUtils.getNullEmptyConvert__(item.getAddress()));
        helper.setText(R.id.tv_deliver_address_details, mBuilder);

        //是否默认地址
        TextView tvDefault = helper.getView(R.id.tv_deliver_address_default);

        if(Constants.STATUS_GLOBAL_YES.equals(item.getIs_default())) {
            tvDefault.setText(R.string.text_default_deliver_address);
            tvDefault.setCompoundDrawablesWithIntrinsicBounds(mCheckDrawable, null, null, null);
        } else {
            tvDefault.setText(R.string.text_set_default_deliver_address);
            tvDefault.setCompoundDrawablesWithIntrinsicBounds(mNormalDrawable, null, null, null);
        }

        //编辑删除监听
        helper.addOnClickListener(R.id.tv_deliver_address_edit, R.id.tv_deliver_address_delete);
    }
}
